#!/bin/bash

. $TEST_ROOT_DIR/run-test-common

# This test uses a large dataset which is not part of the CNTK repository itself
# We use the dataset from an external location specified using an environment variable
if [[ "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" == "" || ! -d "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" ]]; then
    echo 'This test uses external data that is not part of the CNTK repository. Environment variable CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY must be set to point to the external test data location'
    exit 1
fi

# Copy the test data to the test run directory
if [ "$OS" == "Windows_NT" ]; then
    TestDataDir=$TEST_BIN_DIR/cifar-10-batches-py
else
    TestDataDir=$TEST_BIN_DIR/../lib/cifar-10-batches-py
fi

mkdir $TestDataDir

if [ "$OS" == "Windows_NT" ]; then
    DataSourceDir=`cygpath -au $CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY`/Image/CIFAR/v0
else
    DataSourceDir=$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY/Image/CIFAR/v0        
fi

cp -R $DataSourceDir/cifar-10-batches-py/. $TestDataDir || exit $?

# Set CUDA_VISIBLE_DEVICES to exclude all gpu if running on cpu device
[ "$TEST_DEVICE" == "cpu" ] && export CUDA_VISIBLE_DEVICES=-1

pushd $TestDataDir

if [ "$OS" == "Windows_NT" ]; then
    pushd $TEST_BIN_DIR > /dev/null
    TestDataDirConvert=`cygpath -aw $TestDataDir`
else
# we build all dlls to lib dir on Linux for now
    pushd $TEST_BIN_DIR/../lib > /dev/null
    TestDataDirConvert=$TestDataDir
fi
dotnet CNTKLibraryCSTrainingTest.dll CifarResNetClassifierTest $TestDataDirConvert
popd > /dev/null

# Delete the test data
popd
rm -rf $TestDataDir

exit 0